package com.woniu.carrent.controller;



import cn.dev33.satoken.annotation.SaCheckLogin;
import com.woniu.carrent.pojo.TUser;
import com.woniu.carrent.service.ALiYunOssSevice;
import com.woniu.carrent.util.ResultMessage;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/oss")
@CrossOrigin
@SaCheckLogin
public class ALiYunOssController {

    @Autowired
    private ALiYunOssSevice aLiYunOssSevice;

    /**
     * 上传图片
     */
    @PostMapping("/updateAvatar")
    public ResultMessage updateAvatar(MultipartFile file, TUser tUser) throws IOException {
        //获取当前用户名称
        String username = tUser.getUsername();
        //调用service层获取oss返回访问图片链接
        String url = aLiYunOssSevice.updateAvatar(file,username);
        if (url!=null){
            return ResultMessage.success(url);
        }
        return ResultMessage.fail("上传失败");
    }
}